package cn.dengta.webapp.user.model;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.model.ValueProxyImpl;
import cn.dengta.context.model.Country;
import cn.dengta.context.model.Option;
import lombok.Getter;
import me.codeplayer.util.*;

@Getter
public enum RecruitPhoneArea implements ValueProxyImpl<RecruitPhoneArea, String> {
	/** 菲律宾 */
	PH(0, "63"),
	/** 印尼 */
	ID(1, "62"),
	/** 马来西亚 */
	MY(2, "60"),
	/** 越南 */
	VN(3, "84"),
	/** 印度 */
	IN(4, "91", false),
	/** 泰国 */
	TH(5, "66"),
	/** 韩国 */
	KR(6, "82"),
	/** 日本 */
	JP(7, "81"),
	/** 台湾 */
	TW(8, "886"),
	/** 香港 */
	HK(9, "852"),
	/** 澳门 */
	MO(10, "853"),

	//
	;
	public final Integer index;
	public final String code;
	public final boolean enabled;

	final ValueProxy<RecruitPhoneArea, String> proxy;

	RecruitPhoneArea(Integer index, String code, boolean enabled) {
		this.index = index;
		this.code = code;
		this.enabled = enabled;
		this.proxy = new ValueProxy<>(this, code, name());
	}

	RecruitPhoneArea(Integer value, String code) {
		this(value, code, true);
	}

	public boolean isEnabled() {
		return enabled;
	}

	public static RecruitPhoneArea[] CACHE = ArrayUtil.filter(values(), RecruitPhoneArea::isEnabled);

	public static RecruitPhoneArea of(String value) {
		RecruitPhoneArea area = HK.getValueOf(value);
		return area != null && area.isEnabled() ? area : null;
	}

	public static RecruitPhoneArea findByCountry(String country) {
		if (StringUtil.notEmpty(country)) {
			for (RecruitPhoneArea phoneArea : CACHE) {
				if (phoneArea.name().equals(country)) {
					return phoneArea;
				}
			}
		}
		return null;
	}

	public static List<Option<String>> NATIONAL_FLAG_OPTION;

	public static List<Option<String>> getNationalFlagOption() {
		List<Option<String>> option = NATIONAL_FLAG_OPTION;
		if (option == null) {
			option = Arrays.stream(CACHE).map(t -> new Option<>(Country.of(t.name()).getNationalShortFlag(), t.getLabel(), NumberUtil.getLong(t.code))).collect(Collectors.toList());
			NATIONAL_FLAG_OPTION = option;
		}
		return option;
	}

}
